package com.luwanglin.oss.test1;

public class GenericArray<T> {
    private T[] array;

    @SuppressWarnings("unchecked")
    public GenericArray(int size) {
        array = (T[]) new Object[size];
    }

    public void put(int index, T item) {
        array[index] = item;
    }

    public T get(int index) {
        return array[index];
    }

    // method that exposes the underlying representation
    public T[] rep() {
        return array;
    }

    public static void main(String[] args) {
        int size = 10;
        GenericArray<Integer> gArr = new GenericArray<Integer>(size);

        for (int i = 0; i < size; i++) {
            gArr.put(i, i);
        }
        for (int i = 0; i < size; i++) {
            System.out.println("gArr[" + i + "] = " + gArr.get(i));
        }

        // ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.Integer
//        Integer[] ia = gArr.rep();

        // this is ok
        Object[] arr = gArr.rep();

        System.out.println(arr);
    }
}